package org.example.dao.impl;

import org.example.api.entity.User;
import org.example.api.utils.DruidConnectionUtils;
import org.example.dao.IUserDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Re
 * @Date: 2021/4/17 23:17
 */
public class UserDaoImpl implements IUserDao{
    DruidConnectionUtils druidConnectionUtils=new DruidConnectionUtils();
    @Override
    public boolean addUser(Connection connection, User user) {
        PreparedStatement preparedStatement = null;
        String sql = "insert into usertest(userName,userPassword,userSex) values (?,?,?);";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUserName());
            preparedStatement.setString(2,user.getUserPassword());
            preparedStatement.setString(3,user.getUserSex());
            if (preparedStatement.executeUpdate() == 1){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return false;
    }

    @Override
    public User checkUser(Connection connection, User user) {
        User user1 = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from usertest where userName = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUserName());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                user1 = new User(resultSet.getInt("userId"), resultSet.getString("userName"),resultSet.getString("userPassword"),resultSet.getString("userSex") );
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return user1;
    }

    @Override
    public boolean deleteUser(Connection connection, User user) {
        PreparedStatement preparedStatement = null;
        String sql = "delete from usertest where userId = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,user.getUserId());
            if (preparedStatement.executeUpdate() == 1){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return false;
    }

    @Override
    public boolean updateUser(Connection connection,User user) {
        PreparedStatement preparedStatement = null;
        String sql = "update usertest set userPassword = ? where userId = ?;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUserPassword());
            preparedStatement.setInt(2,user.getUserId());
            if (preparedStatement.executeUpdate() == 1){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,null);
        }
        return false;
    }

    @Override
    public List<User> checkAllUser(Connection connection) {
        List<User> users = new ArrayList<>();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from usertest;";
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                users.add(new User(resultSet.getInt("userId"),resultSet.getString("userName"),resultSet.getString("userPassword"),resultSet.getString("userSex")));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            druidConnectionUtils.closeConnection(connection,preparedStatement,resultSet);
        }
        return users;
    }
}
